<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2018/1/24
 * Time: 14:39
 * Desc: 支付完成业务处理 -- 领取优惠券等
 */
namespace app\index\controller;
use think\Controller;
use think\Db;
//use app\comm\controller\WebLogController;
use app\index\controller\Base;
class PayComplete extends Base{
    public function _initialize(){
        parent::_initialize();
        $cur_city = db('city')->where('key',cookie('city_key'))->value('value');        //当前城市

        $this->assign('realname',session('_user_uid_.realname'));
        $this->assign('cur_city',$cur_city);
        $this->assign('url',request()->url(true));

        $sql = "SELECT id,name,logo,cat_id FROM ys_goods AS a WHERE 6> (SELECT COUNT(*) FROM ys_goods WHERE cat_id=a.cat_id AND id>a.id) AND a.is_online=1 ORDER BY a.id DESC";
        $tui_goods = Db::query($sql);
        $this->assign('tui_goods',$tui_goods);
    }

    /**
     * @Author  lingyun
     * @Desc    支付完成领取优惠券
     */
    public function get_coupon(){
        $request = request();
        if($request->isPost()){
            $post = input('post.');
            $uid = session('_user_uid_.uid');       //会员id
            $result = db('user_ycard')->where(['uid'=>$uid,'coupon_id'=>$post['id']])->find();      //判断是否已经领取了优惠券
            $coupon = db('store_coupon')->where(['id'=>$post['id'],'status'=>1,'is_deleted'=>0])->find();
            if(!empty($result)){
                $this->error('该优惠券已经领取，请勿重复领取','');
            }

            if(empty($coupon)){
                $this->error('找不到给优惠券信息，请领取其它优惠券','');
            }
            //领取优惠券
            $data_arr = [
                'store_id'=>$coupon['store_id'],
                'uid'=>$uid,
                'coupon_id'=>$post['id'],
                'receive_order_id'=>$post['receive_order_id'],      //发放优惠券的订单id
                'type'=>0,
                'cat_id'=>$coupon['cat_id'],
                'condition'=>$coupon['condition'],
                'morethan'=>$coupon['morethan'],
                'reduce'=>$coupon['reduce'],
                'use_start_time'=>$coupon['use_start_time'],
                'use_end_time'=>$coupon['use_end_time'],
                'addtime'=>time(),
                'price'=>$coupon['price'],
                'value'=>$coupon['value'],
                'is_used'=>0,
                'status'=>0
            ];
            //领取优惠券
            $res = db('user_ycard')->data($data_arr)->insert();
            //领取数量加一
            db('store_coupon')->where('id',$post['id'])->setInc('send_num',1);
            $res>0?$this->success('领取成功',''):$this->error('领取失败','');

        }else{
            $post = input('param.');
            $out_trade_no = $post['out_trade_no'];
            $order = db('order')->where('order_sn',$out_trade_no)->find();

            if($order['pay_status'] == 0){
                $this->assign('pay_status',0);
            }else{
                $this->assign('pay_status',1);
            }

            //获取顶部信息
//        halt($order['receive_coupon_id']);
            $my_coupon = db('user_ycard')->where(['uid'=>session('_user_uid_.uid'),'store_id'=>cookie('store_id')])->column('coupon_id');
            $coupon = db('store_coupon')->where('id','in',trim($order['receive_coupon_id'],','))->select();

            if($order['receive_coupon_id'] != ''){
                $this->assign('result',1);
            }else{
                $this->assign('result',0);
            }

            $category = db('goods_category')->where(['status'=>1,'is_deleted'=>0,'pid'=>0])->select();

            $this->assign('category',$category);
            $this->assign('order_id',$order['id']);
            $this->assign('coupon',$coupon);
            $this->assign('my_coupon',$my_coupon);
            $this->assign('receive_order_id',$order['id']);
            $this->assign('point',floor($order['final_amount']/2));
            return $this->fetch('get_coupon');
        }
    }

}

